package bb.lanxing.activity.discover;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.adapter.discover.BbsTabAdapter;
import bb.lanxing.model.database.BbsType;
import bb.lanxing.network.BiCiCallback;
import bb.lanxing.network.MyHttpClient;
import com.google.android.material.tabs.TabLayout;
import java.io.IOException;
import java.util.ArrayList;
import okhttp3.Request;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class BbsListActivity extends BaseActivity
{
  private TabLayout tabLayout;
  private ViewPager viewPager;
  private BbsTabAdapter bbsTabAdapter;
  private final ArrayList<BbsType> tabList = new ArrayList<>();

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bbs_list);

    ImageView back = findViewById(R.id.back);
    back.setOnClickListener(v -> onBackPressed());
    ImageView add = findViewById(R.id.add);
    add.setOnClickListener(v -> startActivity(new Intent(this, BbsCreateActivity.class)));

    tabLayout = findViewById(R.id.tabLayout);
    viewPager = findViewById(R.id.pager);

    BbsType bbsType = new BbsType();
    bbsType.typeName = getString(R.string.st_all);
    bbsType.typeId = 0;
    tabList.add(bbsType);
    bbsTabAdapter = new BbsTabAdapter(getSupportFragmentManager(), tabList);
    viewPager.setAdapter(bbsTabAdapter);
    tabLayout.setupWithViewPager(viewPager);
    loadData();
  }

  private void loadData()
  {
    BiCiCallback biCiCallback = new BiCiCallback()
    {
      @Override
      public void onFailure(Request request, IOException e)
      {
        super.onFailure(request, e);
      }

      @Override
      public void onResponseString(@NonNull String string)
      {
        if (!TextUtils.isEmpty(string))
        {
          try
          {
            JSONObject jsonObject = new JSONObject(string);
            int code = jsonObject.optInt("code");
            if (200 == code)
            {
              try
              {
                JSONArray jSONArray = new JSONObject(string).getJSONArray("rows");
                for (int i = 0; i < jSONArray.length(); i++)
                {
                  BbsType createByJson = BbsType.createByJson(jSONArray.getJSONObject(i));
                  tabList.add(createByJson);
                }
              } catch (JSONException e)
              {
                e.printStackTrace();
              }
              runOnUiThread(() -> {
                bbsTabAdapter = new BbsTabAdapter(getSupportFragmentManager(), tabList);
                viewPager.setAdapter(bbsTabAdapter);
                tabLayout.setupWithViewPager(viewPager);
              });
            }
          } catch (JSONException e)
          {
            throw new RuntimeException(e);
          }
        } else
        {
          toast("获取标题失败");
        }
      }
    };
    MyHttpClient.getForumTypeList(biCiCallback);
  }
}